package com.haredot.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@MapperScan(value = "com.haredot.mapper")
@PropertySource(value = "classpath:application.properties")
@EnableTransactionManagement
public class MyBatisConfig implements EnvironmentAware {

    private Environment environment ;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment ;
    }

    /**
     * 配置数据库连接池
     * @return
     */
    @Bean
    public DataSource dataSource(){

        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(environment.getProperty("spring.jdbc.driver-class-name"));
        ds.setUrl(environment.getProperty("spring.jdbc.url"));
        ds.setUsername(environment.getProperty("spring.jdbc.username"));
        ds.setPassword(environment.getProperty("spring.jdbc.password"));
        ds.setMaxActive(environment.getProperty("spring.jdbc.max-active", int.class));
        ds.setMinIdle(environment.getProperty("spring.jdbc.min-idle", int.class));

        return ds ;
    }

    /**
     * 整合 SqlSessionFactory
     * @param dataSource
     * @return
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        // 整合数据源
        factoryBean.setDataSource(dataSource);

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        // 整合 mybatis 的映射文件
        Resource[] resources = resolver.getResources("classpath*:mapper/**/*.xml");
        factoryBean.setMapperLocations(resources);

        // 配置 别名
        factoryBean.setTypeAliasesPackage("com.haredot.entity");

        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setCallSettersOnNulls(true);
        configuration.setMapUnderscoreToCamelCase(true);
        // 配置 空处理和 驼峰标记
        factoryBean.setConfiguration(configuration);

        // 设置 pagehelper 分页插件
        factoryBean.setPlugins(new PageInterceptor());

        return factoryBean.getObject();
    }

    /**
     * 配置事务管理器
     * @param dataSource
     * @return
     */
    @Bean
    public TransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}
